﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;

namespace Smartkernel
{
    /// <summary>
    /// 提供系统服务相关功能的操作封装
    /// </summary>
    public static class SmartWinService
    {
        /// <summary>
        /// 系统服务的操作类型枚举
        /// </summary>
        private enum SmartWinServiceOperateType
        {
            /// <summary>
            /// 启动
            /// </summary>
            Start,
            /// <summary>
            /// 停止
            /// </summary>
            Stop,
            /// <summary>
            /// 继续
            /// </summary>
            Continue,
            /// <summary>
            /// 暂停
            /// </summary>
            Pause
        }
        /// <summary>
        /// 获得当前系统的Windows服务列表
        /// </summary>
        public static List<ServiceController> AllServices
        {
            get
            {
                return new List<ServiceController>(ServiceController.GetServices());
            }
        }
        /// <summary>
        /// 获得当前系统的Windows服务名称列表
        /// </summary>
        public static List<string> AllServicesName
        {
            get
            {
                var linq = from serviceController in AllServices select serviceController.ServiceName;
                return linq.ToList<string>();
            }
        }
        /// <summary>
        /// 启动服务，注意，如果服务是禁用状态是无法启动的
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        public static void Start(string serviceName)
        {
            ChangeState(serviceName, SmartWinServiceOperateType.Start);
        }
        /// <summary>
        /// 继续服务
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        public static void Continue(string serviceName)
        {
            ChangeState(serviceName, SmartWinServiceOperateType.Continue);
        }
        /// <summary>
        /// 暂停服务
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        public static void Pause(string serviceName)
        {
            ChangeState(serviceName, SmartWinServiceOperateType.Pause);
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        public static void Stop(string serviceName)
        {
            ChangeState(serviceName, SmartWinServiceOperateType.Stop);
        }
        /// <summary>
        /// 改变服务状态的操作
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        /// <param name="smartWinServiceOperateType">服务操作的类型</param>
        private static void ChangeState(string serviceName, SmartWinServiceOperateType smartWinServiceOperateType)
        {
            foreach (ServiceController serviceController in AllServices)
            {
                //如果存在指定的服务，则根据新状态进行调整。同时
                if (serviceController.ServiceName.ToLower() == serviceName.ToLower())
                {
                    try
                    {
                        switch (smartWinServiceOperateType)
                        {
                            case SmartWinServiceOperateType.Start:
                                serviceController.Start();
                                break;
                            case SmartWinServiceOperateType.Stop:
                                serviceController.Stop();
                                break;
                            case SmartWinServiceOperateType.Pause:
                                serviceController.Pause();
                                break;
                            case SmartWinServiceOperateType.Continue:
                                serviceController.Continue();
                                break;
                        }
                    }
                    catch (Exception)
                    {

                    }
                    break;
                }
            }
        }
        /// <summary>
        /// 获得服务的状态信息，注意如果服务不存在，则返回ServiceControllerStatus.Stopped，并不会抛出异常
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        /// <returns>状态信息</returns>
        public static ServiceControllerStatus GetServiceStatus(string serviceName)
        {
            foreach (ServiceController serviceController in AllServices)
            {
                if (serviceController.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return serviceController.Status;
                }
            }
            return ServiceControllerStatus.Stopped;
        }
        /// <summary>
        /// 判断服务是否存在，注意，服务名称不区分大小写
        /// </summary>
        /// <param name="serviceName">服务名称，不区分大小写</param>
        /// <returns>是否存在：true＝存在，false＝不存在</returns>
        public static bool Exists(string serviceName)
        {
            bool isExists = false;
            AllServicesName.ForEach
            (
                delegate(string name)
                {
                    if (name.ToLower() == serviceName.ToLower())
                    {
                        isExists = true;
                        return;
                    }
                }
            );
            return isExists;
        }
        /// <summary>
        /// 获得某种状态的服务列表
        /// </summary>
        /// <param name="serviceControllerStatus">状态</param>
        /// <returns>服务的列表</returns>
        public static List<ServiceController> GetServices(ServiceControllerStatus serviceControllerStatus)
        {
            var linq = from serviceController in AllServices where serviceController.Status == serviceControllerStatus select serviceController;
            return linq.ToList<ServiceController>();
        }
        /// <summary>
        /// 获得某种状态的服务名称列表
        /// </summary>
        /// <param name="serviceControllerStatus">状态</param>
        /// <returns>服务名称的列表</returns>
        public static List<string> GetServicesName(ServiceControllerStatus serviceControllerStatus)
        {
            var linq = from serviceController in AllServices where serviceController.Status == serviceControllerStatus select serviceController.ServiceName;
            return linq.ToList<string>();
        }
    }
}
